﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类House_KeyUsedInfo。
    /// </summary>
    public class House_KeyUsedInfo
    {
        public House_KeyUsedInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid KeyId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from House_KeyUsedInfo where KeyId=@KeyId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "KeyId", DbType.Guid, KeyId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.House_KeyUsedInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into House_KeyUsedInfo(");
            strSql.Append("KeyId,InOrOut,ActionDay,keyUserId,keyUserName,Remarks,AddPersonId,HouseAddPerson,AddTime,KeyReceiptionId,HouseAddress,HouseCode,keyUserDepartment,keyUserCode,HouseDepart,CreatTime)");

            strSql.Append(" values (");
            strSql.Append("@KeyId,@InOrOut,@ActionDay,@keyUserId,@keyUserName,@Remarks,@AddPersonId,@HouseAddPerson,@AddTime,@KeyReceiptionId,@HouseAddress,@HouseCode,@keyUserDepartment,@keyUserCode,@HouseDepart,@CreatTime)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "KeyId", DbType.Guid, model.KeyId);
            db.AddInParameter(dbCommand, "InOrOut", DbType.Int32, model.InOrOut);
            db.AddInParameter(dbCommand, "ActionDay", DbType.DateTime, model.ActionDay);
            db.AddInParameter(dbCommand, "keyUserId", DbType.Guid, model.keyUserId);
            db.AddInParameter(dbCommand, "keyUserName", DbType.AnsiString, model.keyUserName);
            db.AddInParameter(dbCommand, "Remarks", DbType.AnsiString, model.Remarks);
            db.AddInParameter(dbCommand, "AddPersonId", DbType.Guid, model.AddPersonId);
            db.AddInParameter(dbCommand, "HouseAddPerson", DbType.AnsiString, model.HouseAddPerson);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "KeyReceiptionId", DbType.Guid, model.KeyReceiptionId);
            db.AddInParameter(dbCommand, "HouseAddress", DbType.AnsiString, model.HouseAddress);
            db.AddInParameter(dbCommand, "HouseCode", DbType.String, model.HouseCode);
            db.AddInParameter(dbCommand, "keyUserDepartment", DbType.String, model.keyUserDepartment);
            db.AddInParameter(dbCommand, "keyUserCode", DbType.String, model.keyUserCode);
            db.AddInParameter(dbCommand, "HouseDepart", DbType.String, model.HouseDepart);
            db.AddInParameter(dbCommand, "CreatTime", DbType.DateTime, model.CreatTime);
            db.ExecuteNonQuery(dbCommand);
            Entity.House_KeyReceiptionInfo house_KeyReceiptionInfo = DAL.House_KeyReceiptionInfo.GetModel(model.KeyReceiptionId);
            if (house_KeyReceiptionInfo != null)
            {
                house_KeyReceiptionInfo.Status = model.InOrOut;
                DAL.House_KeyReceiptionInfo.Update(house_KeyReceiptionInfo);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.House_KeyUsedInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update House_KeyUsedInfo set ");
            strSql.Append("InOrOut=@InOrOut,");
            strSql.Append("ActionDay=@ActionDay,");
            strSql.Append("keyUserId=@keyUserId,");
            strSql.Append("keyUserName=@keyUserName,");
            strSql.Append("Remarks=@Remarks,");
            strSql.Append("AddPersonId=@AddPersonId,");
            strSql.Append("HouseAddPerson=@HouseAddPerson,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("KeyReceiptionId=@KeyReceiptionId,");
            strSql.Append("HouseAddress=@HouseAddress,");
            strSql.Append("HouseCode=@HouseCode,");
            strSql.Append("keyUserDepartment=@keyUserDepartment,");
            strSql.Append("keyUserCode=@keyUserCode,");
            strSql.Append("HouseDepart=@HouseDepart,");
            strSql.Append("CreatTime=@CreatTime");
            strSql.Append(" where KeyId=@KeyId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "KeyId", DbType.Guid, model.KeyId);
            db.AddInParameter(dbCommand, "InOrOut", DbType.Int32, model.InOrOut);
            db.AddInParameter(dbCommand, "ActionDay", DbType.DateTime, model.ActionDay);
            db.AddInParameter(dbCommand, "keyUserId", DbType.Guid, model.keyUserId);
            db.AddInParameter(dbCommand, "keyUserName", DbType.AnsiString, model.keyUserName);
            db.AddInParameter(dbCommand, "Remarks", DbType.AnsiString, model.Remarks);
            db.AddInParameter(dbCommand, "AddPersonId", DbType.Guid, model.AddPersonId);
            db.AddInParameter(dbCommand, "HouseAddPerson", DbType.AnsiString, model.HouseAddPerson);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "KeyReceiptionId", DbType.Guid, model.KeyReceiptionId);
            db.AddInParameter(dbCommand, "HouseAddress", DbType.AnsiString, model.HouseAddress);
            db.AddInParameter(dbCommand, "HouseCode", DbType.String, model.HouseCode);
            db.AddInParameter(dbCommand, "keyUserDepartment", DbType.String, model.keyUserDepartment);
            db.AddInParameter(dbCommand, "keyUserCode", DbType.String, model.keyUserCode);
            db.AddInParameter(dbCommand, "HouseDepart", DbType.String, model.HouseDepart);
            db.AddInParameter(dbCommand, "CreatTime", DbType.DateTime, model.CreatTime);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid KeyId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from House_KeyUsedInfo ");
            strSql.Append(" where KeyId=@KeyId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "KeyId", DbType.Guid, KeyId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_KeyUsedInfo GetModel(Guid KeyId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select KeyId,InOrOut,ActionDay,keyUserId,keyUserName,Remarks,AddPersonId,HouseAddPerson,AddTime,KeyReceiptionId,HouseAddress,HouseCode,keyUserDepartment,keyUserCode,HouseDepart,CreatTime from House_KeyUsedInfo ");
            strSql.Append(" where KeyId=@KeyId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "KeyId", DbType.Guid, KeyId);
            Chenxin.Lib.House.Entity.House_KeyUsedInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select KeyId,InOrOut,ActionDay,keyUserId,keyUserName,Remarks,AddPersonId,HouseAddPerson,AddTime,KeyReceiptionId,HouseAddress,HouseCode,keyUserDepartment,keyUserCode,HouseDepart,CreatTime ");
            strSql.Append(" FROM House_KeyUsedInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);

            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.House_KeyUsedInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select KeyId,InOrOut,ActionDay,keyUserId,keyUserName,Remarks,AddPersonId,HouseAddPerson,AddTime,KeyReceiptionId,HouseAddress,HouseCode,keyUserDepartment,keyUserCode,HouseDepart,CreatTime ");
            strSql.Append(" FROM House_KeyUsedInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.House_KeyUsedInfo> list = new List<Chenxin.Lib.House.Entity.House_KeyUsedInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_KeyUsedInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.House_KeyUsedInfo model = new Chenxin.Lib.House.Entity.House_KeyUsedInfo();
            object ojb;
            ojb = dataReader["KeyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.KeyId = new Guid(ojb.ToString());
            }
            ojb = dataReader["InOrOut"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.InOrOut = (int)ojb;
            }
            ojb = dataReader["ActionDay"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ActionDay = (DateTime)ojb;
            }
            ojb = dataReader["keyUserId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.keyUserId = new Guid(ojb.ToString());
            }
            model.keyUserName = dataReader["keyUserName"].ToString();
            model.Remarks = dataReader["Remarks"].ToString();
            ojb = dataReader["AddPersonId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddPersonId = new Guid(ojb.ToString());
            }
            model.HouseAddPerson = dataReader["HouseAddPerson"].ToString();
            ojb = dataReader["AddTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddTime = (DateTime)ojb;
            }
            ojb = dataReader["KeyReceiptionId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.KeyReceiptionId = new Guid(ojb.ToString());
            }
            model.HouseAddress = dataReader["HouseAddress"].ToString();
            model.HouseCode = dataReader["HouseCode"].ToString();
            model.keyUserDepartment = dataReader["keyUserDepartment"].ToString();
            model.keyUserCode = dataReader["keyUserCode"].ToString();
            model.HouseDepart = dataReader["HouseDepart"].ToString();
            ojb = dataReader["CreatTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CreatTime = (DateTime)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}

